package com.fwh.web.controller;

import com.fwh.web.utils.redis.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import redis.clients.jedis.Jedis;

import java.util.Set;

/**
 * @Author: fwh
 * @date 2019/4/11&& 18:10
 */
@Slf4j
@Controller
@RequestMapping("/RedisController")
public class RedisController {
    @Autowired
    private RedisUtil redisUtil;
    
    /**
     * Redis Zadd 命令用于将一个或多个成员元素及其分数值加入到有序集当中
     * @param   
     * @return void  
     * @author:fwh [2019/4/12 && 11:46]
     */  
    @GetMapping("/zadd")
    public void zadd() {
        String key="fwh";
        double score=100d;
        String member="fuwenhao";
        Jedis jedis = redisUtil.getJedis();
        //排序存储 第100名 fuwenhao  取值命令: zrange fwh 0 -1 withscores
        Set<String> keys = jedis.keys(key);
        if (null!=keys&&keys.size()==0){
            Long result = jedis.zadd(key, score, member);
            if (result>0){
                log.info("添加成功");
                Set<String> zrange = jedis.zrange(key, 0, -1);
                log.info("具体值为:{}",zrange);
            }else{
                log.error("添加redis失败!");
            }
        }else{
            log.error("该key:{}存在,请不要重复添加...",key);
        }
        jedis.close();
    }
}
